package com.system.service;

import java.util.ArrayList;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.staff.dao.StaffRepository;
import com.staff.entity.Staff;
import com.system.dao.PermissionRepository;
import com.system.dao.RoleRepository;
import com.system.entity.Permission;
import com.system.entity.Role;

@Service
@Transactional
public class RoleService implements IRoleService
{
	@Autowired
	private RoleRepository roleRepository;
	@Autowired
	private PermissionRepository permissionRepository;
	@Autowired
	private StaffRepository staffRepository;
	
	
	@Override//修改角色普通字段
	public void saveOrUpdate(Role role) {
		// TODO Auto-generated method stub
		if(role.getId()!=0) {
			Role role1=roleRepository.findOne(role.getId());		
			role1.setName(role.getName());
			role1.setDescription(role.getDescription());
			roleRepository.save(role1);
		}
		else {
			roleRepository.save(role);
		}		
	}
	@Override
	public void save(Role role) {
		// TODO Auto-generated method stub	
		roleRepository.save(role);
	}
	
	@Override//保存角色权限方法
	public void saveRolePermission(Long rid,List<Long> pIds) {
		// TODO Auto-generated method stub	
		Role role=roleRepository.findOne(rid);
		role.getPermissionList().clear();
		for(Long pid:pIds) {
			Permission permission=permissionRepository.findOne(pid);
			role.getPermissionList().add(permission);
		}
		roleRepository.save(role);
	}
	
	@Override//保存用户角色方法
	public void saveStaffRole(Long sid,List<Long> rids) {
		// TODO Auto-generated method stub
		Staff staff=staffRepository.findOne(sid);     //先找出用户
		List<Role> roles=roleRepository.findRoleByStaff(sid);  //根据用户ID找出拥有角色
		for(Role role:roles) {
			role.getStaffList().remove(staff);     //将角色里的该用户移除
			roleRepository.save(role);             //重新保存，移除关系
		}
		for(Long rid:rids) {
			Role r=roleRepository.findOne(rid);   //找出角色
			r.getStaffList().add(staff);          //建立关联关系
			roleRepository.save(r);               //保存数据库
		}
	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		Role role=roleRepository.findOne(id);
		if(null!=role) {
			role.setPermissionList(null);
			role.setStaffList(null);
			roleRepository.save(role);
			roleRepository.delete(role);
		}
	}

	@Override
	public void delete(List<Long> ids) {
		// TODO Auto-generated method stub
		List<Role> roleList=(List<Role>) roleRepository.findAll(ids);
		if(roleList!=null) {
			for(Role role:roleList) {
				role.setPermissionList(null);
				role.setStaffList(null);
				roleRepository.save(role);
				roleRepository.delete(role);
			}
		}
	}

	@Transactional(readOnly=true)
	@Override
	public Role findOne(Long id) {
		// TODO Auto-generated method stub
		Role role=roleRepository.findOne(id);
		return role;
	}

	//根据角色Id查找权限
	@Override
	@Transactional(readOnly=true)
	public Role findPermissionsByRoleId(Long id) {
		// TODO Auto-generated method stub
		Role role=roleRepository.findPermissionsByRoleId(id);
		return role;
	}
	
	//根据角色名查找权限
	@Override
	@Transactional(readOnly=true)
	public Role findPermissionsByRoleName(String roleName) {
		// TODO Auto-generated method stub
		Role role=roleRepository.findPermissionsByRoleName(roleName);
		return role;
	}
	//根据用户ID查找角色
	@Override
	@Transactional(readOnly=true)
	public List<Role> findByStaffId(Long id) {
		// TODO Auto-generated method stub
		List<Role> roleList=roleRepository.findRoleByStaff(id);
		return roleList;
	}

	
	@Override
	@Transactional(readOnly=true)
	public Page<Role> findPage(Specification<Role> spec,Pageable pageable) {
		// TODO Auto-generated method stub
		
		return roleRepository.findAll(spec,pageable);
	}
	
	@Override//查找角色权限
	@Transactional(readOnly=true)
	public List<Permission> findRolePermission(Long id){
		List<Permission> permissions1= (List<Permission>) permissionRepository.findAll();
		List<Permission> permissions2=roleRepository.findRolePermissions(id);
		List<Permission> permissions=new ArrayList<Permission>();
		for(Permission p1:permissions1) {
			for(Permission p2:permissions2) {
				if(p1.getId()==p2.getId()) {
					p1.setIsSelect(true);
					break;
				}
				else{
					p1.setIsSelect(false);
				}				
			}
			permissions.add(p1);
		}
		//System.out.println(permissions);
		return permissions;
	}
	
	@Override//找出用户拥有的角色
	public List<Role> findStaffRole(Long id) {
		// TODO Auto-generated method stub
		List<Role> roles1= (List<Role>) roleRepository.findAll();
		List<Role> roles2=roleRepository.findRoleByStaff(id);
		List<Role> roles=new ArrayList<Role>();
		for(Role r1:roles1) {
			for(Role r2:roles2) {
				if(r1.getId()==r2.getId()) {
					r1.setIsSelect(true);
					break;
				}
				else{
					r1.setIsSelect(false);
				}				
			}
			roles.add(r1);
		}
		//System.out.println(permissions);
		return roles;
	}

}
